package com.huaxiafinance.management.controller;


import com.huaxiafinance.management.dto.request.PartnerGradListeDto;
import com.huaxiafinance.management.dto.request.PartnerGradeDto;
import com.huaxiafinance.management.dto.request.PartnerGradeEnableDto;
import com.huaxiafinance.management.dto.request.PartnerGradeSortDto;
import com.huaxiafinance.management.dto.response.PartnerWithGrade;
import com.huaxiafinance.management.exception.BusinessException;
import com.huaxiafinance.management.model.PartnerGrade;
import com.huaxiafinance.management.service.PartnerGradeService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @author yangtong
 * @create 2017-12-04 18:03
 **/
@Controller("partnerGradeController")
@RequestMapping("/partnerGrade")
public class PartnerGradeController {

  @Autowired
  private PartnerGradeService partnerGradeService;


  private static final Logger log = Logger.getLogger(PartnerGradeController.class);


  /**
   * 查询所有的合伙人等级信息
   *
   * @return ResponseMap /*
   */
  @RequestMapping(value = "/queryAllPartnerGrade", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateSort() throws Exception {
    {
      MessageResult messageResult = new MessageResult();
      List<PartnerWithGrade> partnerGrades = partnerGradeService.getAllPartnerGrade();
      HashMap<String, Object> map = new HashMap<>();
      map.put("partnerGrades", partnerGrades);
      messageResult.setData(map);
      messageResult.setSuccessResult();
      return messageResult.getAsJSON();
    }
  }


  @RequestMapping(value = "/add", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String add(HttpServletRequest request,
      @Valid @RequestBody PartnerGradeDto dto, BindingResult bindingResult) throws Exception {

    log.info("添加条件等级请求参数=======================>" + dto);
    MessageResult messageResult = new MessageResult();

    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
      return messageResult.getAsJSON();
    }

    int count = partnerGradeService.add(dto);

    if (count > 0) {

      messageResult.setResultMessage(ServiceCode.SUCCESS);
      messageResult.setSuccessResult();
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/edit", produces = "application/json; charset=utf-8", method = RequestMethod.POST)
  public @ResponseBody
  String edit(@Valid @RequestBody PartnerGradeDto dto, BindingResult bindingResult)
      throws Exception {
    log.info("修改条件等级请求参数" + dto);
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
      return messageResult.getAsJSON();
    }

    try {
      if (dto == null || dto.getId() == null) {
        throw new BusinessException("规则等级id不能为空");
      }

      int count = partnerGradeService.edit(dto);

      if (count > 0) {
        messageResult.setResultMessage(ServiceCode.SUCCESS);
        messageResult.setSuccessResult();
      } else {
        messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
      }
    } catch (BusinessException ex) {
      messageResult.setFailureResult(ex);
    }

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/detail", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
  public @ResponseBody
  String edit(Integer gradeId) throws Exception {
    log.info("查询条件等级详细请求参数" + gradeId);
    MessageResult messageResult = new MessageResult();

    try {
      if (gradeId == null) {
        throw new BusinessException("规则等级id不能为空");
      }

      PartnerGrade bo = partnerGradeService.detail(gradeId);

      if (bo != null) {
        messageResult.setData(bo);
        messageResult.setResultMessage(ServiceCode.SUCCESS);
        messageResult.setSuccessResult();
      } else {
        messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
      }
    } catch (BusinessException ex) {
      messageResult.setFailureResult(ex);
    }

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/list", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
  public @ResponseBody
  String list(@Valid @ModelAttribute("dto") PartnerGradListeDto dto) throws Exception {
    log.info("查询条件等级列表请求参数" + dto);
    MessageResult messageResult = new MessageResult();

    List<PartnerGrade> boList = partnerGradeService.list(dto);

    if (boList != null) {
      messageResult.setData(boList);
      messageResult.setRows(partnerGradeService.countlist(dto));
      messageResult.setResultMessage(ServiceCode.SUCCESS);
      messageResult.setSuccessResult();
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/updateSort", produces = "application/json; charset=utf-8", method = RequestMethod.POST)
  public @ResponseBody
  String updateSort(@RequestBody PartnerGradeSortDto ids) throws Exception {
    log.info("修改条件等级顺序请求参数" + ids);
    MessageResult messageResult = new MessageResult();

    boolean temp = partnerGradeService.updateSort(ids);

    if (temp) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
      messageResult.setSuccessResult();
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/updateEnable", produces = "application/json; charset=utf-8", method = RequestMethod.POST)
  public @ResponseBody
  String updateSort(@RequestBody PartnerGradeEnableDto dto) throws Exception {
    log.info("修改条件等级可用停用请求参数" + dto);
    MessageResult messageResult = new MessageResult();

    try {
      if (dto == null || dto.getGradeId() == null) {
        throw new BusinessException("规则等级id不能为空");
      }
      if (dto.getIsEnable() == null) {
        throw new BusinessException("规则等级控制不能为空");
      }

      int temp = partnerGradeService
          .updateEnable(dto.getGradeId(), dto.getIsEnable(), dto.getUserId());

      if (temp != 0) {
        messageResult.setResultMessage(ServiceCode.SUCCESS);
        messageResult.setSuccessResult();
      } else {
        messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
      }
    } catch (BusinessException ex) {
      messageResult.setFailureResult(ex);
    }

    return messageResult.getAsJSON();

  }

}

